package com.hospital.phyexam.impl;

import com.alibaba.fastjson.JSON;
import com.hospital.login.entity.AdminOffice;
import com.hospital.phyexam.dao.AdminOfficeMapper;
import com.hospital.phyexam.dao.PhyexamProjectMapper;
import com.hospital.phyexam.dao.RelationPhyBatchMapper;
import com.hospital.phyexam.entity.PhyexamProject;
import com.hospital.phyexam.service.AdminOfficeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminOfficeServiceImpl implements AdminOfficeService {
    @Autowired
    AdminOfficeMapper adminOfficeMapper;
    @Autowired
    PhyexamProjectMapper phyexamProjectMapper;
    @Autowired
    RelationPhyBatchMapper relationMapper;

    public Map getAll(AdminOffice adminOffice){
        Map<String,String>map=new HashMap<>();
        map.put("id",adminOffice.getId().toString());
        map.put("ofcNum",adminOffice.getOfcNum());
        map.put("ofcName",adminOffice.getOfcName());
        map.put("ofcRemark",adminOffice.getOfcRemark());
        return map;
    }


    @Override
    public String getAllAdmin(){
        List<Map<String,String>>resaultmap=new ArrayList<>();
        List<AdminOffice>adminOffices=adminOfficeMapper.selectAll();
        for (AdminOffice adminOffice:adminOffices){
            resaultmap.add(getAll(adminOffice));

        }
        System.out.println(resaultmap);
        return JSON.toJSONString(resaultmap);
    }

    @Override
    public String getAllAdminOffice(){
        List<Map<String,String>>resaultmap=new ArrayList<>();

        List<AdminOffice>adminOffices=adminOfficeMapper.selectAll();
        for (AdminOffice adminOffice:adminOffices){
            Map<String,String>map=new HashMap<>();
            map.put("id",adminOffice.getId().toString());
            map.put("ofcName",adminOffice.getOfcName());
            resaultmap.add(map);

        }
        System.out.println(resaultmap);
        return JSON.toJSONString(resaultmap);
    }
    @Override
    public String insert(String num,String name,String remark){
        AdminOffice adminOffice=new AdminOffice();
        adminOffice.setOfcNum(num);
        adminOffice.setOfcName(name);
        adminOffice.setOfcRemark(remark);
        adminOfficeMapper.insert(adminOffice);
        return getAllAdmin();
    }

    @Override
    public String update(int id,String num,String name,String remark){
        AdminOffice adminOffice=adminOfficeMapper.selectByPrimaryKey(id);
        adminOffice.setOfcNum(num);
        adminOffice.setOfcName(name);
        adminOffice.setOfcRemark(remark);
        adminOfficeMapper.updateByPrimaryKey(adminOffice);
        return getAllAdmin();
    }
    @Override
    public String delete(int id){
        if (phyexamProjectMapper.selectByOfficeId(id)!=null){
            List<PhyexamProject>phyexamProjects=phyexamProjectMapper.selectByOfficeId(id);
            for (PhyexamProject phyexamProject:phyexamProjects){
                if (relationMapper.selectByProId(phyexamProject.getId())!=null){
                    relationMapper.deleteByProId(phyexamProject.getId());
                }
            }
            phyexamProjectMapper.deleteByOfficeId(id);
        }
        adminOfficeMapper.deleteByPrimaryKey(id);
        return getAllAdmin();
    }
}
